package com.zsyh.jiujiu.utils;

import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.CommonAdapter;
import com.zsyh.jiujiu.adepter.shengjiAdepter;
import com.zsyh.jiujiu.bean.DizhiBean;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TestAddressProvider implements AddressProvider {
    AppCompatActivity mContext;
    public TestAddressProvider(AppCompatActivity mContext){
        this.mContext=mContext;
    }




    @Override
    public void provideProvinces(AddressReceiver<Province> addressReceiver) {
        sheng("",addressReceiver);
        //addressReceiver.send(Collections.singletonList(province));
    }

    @Override
    public void provideCitiesWith(int provinceId, AddressReceiver<City> addressReceiver) {
        chengshi("",addressReceiver);
    }

    @Override
    public void provideCountiesWith(int cityId, AddressReceiver<County> addressReceiver) {
        County county = new County();
        county.city_id = cityId;
        county.id = 3;
        county.name = "测试用市县";
        addressReceiver.send(Collections.singletonList(county));
    }

    @Override
    public void provideStreetsWith(int countyId, AddressReceiver<Street> addressReceiver) {
        Street street = new Street();
        street.county_id = countyId;
        street.id = 4;
        street.name = "测试用市县";
        addressReceiver.send(Collections.singletonList(street));
    }


    /**
     * 获取省份
     */
    public void sheng(String id,AddressReceiver<Province> addressReceiver){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        List<Province> list=new ArrayList<>();
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            Province province = new Province();
                            province.id = Integer.valueOf(dizhiBean.getData().get(i).getId());
                            province.name = dizhiBean.getData().get(i).getAreaName();
                            list.add(province);
                        }
                        addressReceiver.send(list);

                    }
                });
    }


    /**
     * 获取城市
     */
    public void chengshi(String id,AddressReceiver<City> addressReceiver){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getAreaByPid(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(mContext,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dizhi",JsonStr);
                        Gson gson=new Gson();
                        DizhiBean dizhiBean=gson.fromJson(JsonStr,DizhiBean.class);
                        List<City> list=new ArrayList<>();
                        for(int i=0;i<dizhiBean.getData().size();i++){
                            City city = new City();
                            city.id = Integer.valueOf(dizhiBean.getData().get(i).getId());
                            city.name = dizhiBean.getData().get(i).getAreaName();
                            list.add(city);
                        }
                        addressReceiver.send(list);

                    }
                });
    }

}
